<template>
  <div>
    <el-form class="query-table-form" @submit.native.prevent :inline="true" :model="params">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="项目名称查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.corp_name"
          placeholder="负责人查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          clearable
          placeholder="地址查询"
          auto-top
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <c-select
          :options="enumUnStatusSelect"
          v-model="params['expire_status.eq']"
          clearable
          placeholder="项目状态"
          @change="getData()"
        ></c-select>
      </el-form-item>
      <el-form-item>
        <c-select
          :options="enumGuatype"
          v-model="params.gua_type"
          clearable
          placeholder="业务类型"
          @change="getData()"
        ></c-select>
      </el-form-item>

      <el-form-item>
        <c-select
          :options="enumUnGuaType"
          v-model="params.un_gua_type"
          clearable
          placeholder="解保类型"
          @change="getData()"
        ></c-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>

      <el-form-item class="float-right">
        <el-button icon="el-icon-edit" @click="handleToUn()">解保登记</el-button>
      </el-form-item>
    </el-form>

    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <el-tooltip effect="light" :content="scope.row.address">
          <div class="text-truncate">{{ scope.row.address }}</div>
        </el-tooltip>
      </template>
      <template v-slot:name="scope">
        <el-popover placement="top-start" title="项目经理" trigger="hover">
          <div>
            <div>{{ `A角： ${scope.row.res_a_name || '-'}；` }}</div>
            <div class="mb-2">{{ `电话： ${scope.row.res_a_phone || '-'}` }}</div>
            <div>{{ `B角： ${scope.row.res_b_name || '-'}；` }}</div>
            <div>{{ `电话： ${scope.row.res_b_phone || '-'}` }}</div>
          </div>
          <div slot="reference">
            <div class="status-label badge-info text-left w-100">
              <div>A角:{{ scope.row.res_a_name }}</div>
              <div>B角:{{ scope.row.res_b_name }}</div>
            </div>
          </div>
        </el-popover>
      </template>

      <template v-slot:status="scope">
        <span class="badge" :class="`badge-${scope.row.statusStyle}`">
          {{ scope.row.statusLable }}
        </span>
        <div>
          <small v-if="+scope.row.expire_status === 4" class="text-primary">
            {{ scope.row.unGuaType }}
          </small>
        </div>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>
    <postloan-file :visible.sync="reportVisible" :related-data="fileData"></postloan-file>
    <attach-cmpt :visible.sync="attachVisible" :customer-related-id="attachRelatedId"></attach-cmpt>
    <project-un-loan
      :visible.sync="editVisible"
      :query-id="editId"
      @success="getData()"
    ></project-un-loan>

    <un-list-dialog :visible.sync="advanceVisible" @success="getData"></un-list-dialog>

    <flow-dialog
      title="解保审批流程"
      :visible.sync="flowVisible"
      flow-key="UN_GUA_APPROVAL"
    ></flow-dialog>
  </div>
</template>

<script>
import { enumGuatype, enumProcessStatus } from '@/credit/plugins/enumTown'
import { baseTableColumns } from '@/credit/views/county/unionProvince/Approve'
import baseMixins from '@/credit/views/city/bus/normal/mixins'

import SelectAddress from '@/components/select/SelectAddress'
// import LetterView from '@/credit/views/county/mission/components/LetterView'
// import RegisterLoanNotice from '@/credit/views/county/mission/RegisterLoanNotice'
import PostloanFile from '@/credit/views/county/loan/component/ViewDialog'
import AttachCmpt from '@/credit/views/city/program/Attach'
// import AttachView from '@/credit/components/AttachView'
import ProjectUnLoan from '@/credit/views/county/loan/component/un/MainDialog'
import { deltaDay } from '@/common/plugins/tools/date'
import UnListDialog from '@/credit/views/county/loan/component/advance/ListDialog'
import FlowDialog from '@/credit/components/flow/ViewDialog'
import { enumUnGuaType } from '@/credit/views/county/loan/component/un/index'
export const DATE_EXPIRE = 90

const enumUnStatus = {
  0: { label: '未到期', style: 'info' },
  1: { label: '即将到期', style: 'warning' },
  2: { label: '已到期', style: 'warning' },
  3: { label: '到期未解保', style: 'danger' },
  4: { label: '已解保', style: 'success' },
}
export default {
  components: {
    SelectAddress,
    // LetterView,
    // RegisterLoanNotice,
    AttachCmpt,
    // AttachView
    PostloanFile,
    ProjectUnLoan,
    UnListDialog,
    FlowDialog,
  },
  mixins: [baseMixins],
  props: {},
  data() {
    return {
      enumGuatype,
      enumUnGuaType,
      attachVisible: false,
      attachRelatedId: '',
      params: {
        index: 1,
        size: 10,
        customer_name: '',
        corp_name: '',
        address_code: '',
        // 'confuse_status.neq': 3,
        // 'loan_approval_status.eq': 3,
        // 'load_beh_status.eq': 1,
        'gua_type.eq': '',
        'channel_type.eq': '',
      },
      order: {
        // N1expire_status: 'asc',
        // N2load_end_date: 'desc',
        load_end_date: 'asc',
      },
      useRegisterTable: true,
      tableName: 'un_gua_apply_view',
      tableColumns: [
        ...baseTableColumns,
        { attrs: { label: '放款金额(万元)', prop: 'reply_funds', align: 'center' } },
        { attrs: { label: '在保余额(万元)', prop: 'left_funds', align: 'center' } },
        { attrs: { label: '放款时间', prop: 'startDate', align: 'center', width: 100 } },
        { attrs: { label: '到期时间', prop: 'endDate', align: 'center', width: 100 } },
        {
          attrs: { label: '放款银行', prop: 'bank_name', width: 140 },
          process: v => v || '-',
        },
        // { attrs: { label: '审批模式', prop: 'processLabel', width: 80, align: 'center' } },
        { attrs: { label: '项目经理', prop: 'resName', width: 100 }, slot: 'name' },
        { attrs: { label: '业务模式', prop: 'guaType', width: 100, align: 'center' } },
        {
          attrs: { label: '项目状态', prop: 'statusLable', width: 100, align: 'center' },
          slot: 'status',
        },
        {
          attrs: {
            label: '剩余到期天数',
            prop: 'endDay',
            width: 100,
            align: 'center',
          },
          process: v => (+v > 0 ? v : '已到期'),
        },
        {
          attrs: { label: '待审核人员', prop: 'task_person_name', align: 'center', fixed: 'right' },
        },
        {
          attrs: { label: '审批流程', align: 'center', width: 80, fixed: 'right' },
          operations: [{ content: '流程', icon: 'fa fa-sitemap', event: this.handleFlow }],
        },
        {
          attrs: { label: '操作', align: 'center', width: '100px', fixed: 'right' },
          operations: [
            {
              content: '解保审批',
              icon: 'fa fa-legal',
              event: this.hanleEdit,
              ifRender: row =>
                (+row.un_gua_status === 0 && [row.res_a, row.res_b].includes(this.userId)) ||
                row.task_person === this.userId ||
                +row.is_history === 1,
            },
            {
              content: '查看',
              icon: 'fa fa-search',
              event: this.hanleView,
            },
          ],
        },
      ],
      dateFmt: 'yyyy-MM-dd',

      activeName: 'letter',

      reportVisible: false,
      fileData: {},

      selectStatus: '',

      editVisible: false,
      editId: '',

      flowData: {},
      tableId: '',
      queryId: '',
      flowVisible: false,
      advanceVisible: false,
    }
  },
  computed: {
    enumUnStatusSelect() {
      return Object.keys(enumUnStatus).map(item => {
        return {
          label: enumUnStatus[item].label,
          value: item,
        }
      })
    },
    routeStatus() {
      return this.$route.query.status || ''
    },
  },
  watch: {},
  created() {
    this.params['service_id.eq'] = this.clientId
    this.params['expire_status.eq'] = this.routeStatus
    this.getData()
  },
  mounted() {},
  methods: {
    handleFlow() {
      this.flowVisible = true
    },
    handleToUn() {
      this.advanceVisible = true
    },
    getParams() {
      return this.params
    },
    hanleEdit(row) {
      this.editVisible = true
      this.editId = row.customer_related_id || ''
    },
    hanleAttach(row) {
      this.attachVisible = true
      this.attachRelatedId = row.customer_related_id || ''
    },
    dataAdapter(item) {
      item.guaType = enumGuatype[item.gua_type] || '-'
      item.endDate = this.$utils.dateFormat(item.load_end_date, this.dateFmt)
      item.startDate = this.$utils.dateFormat(item.load_date, this.dateFmt)
      item.resName = `A角: ${item.res_a_name} B角: ${item.res_b_name}`
      item.processLabel = enumProcessStatus[+item.process_status] || ''
      const statusObject = enumUnStatus[item.expire_status] || {}
      item.statusLable = statusObject.label || ''
      item.statusStyle = statusObject.style || ''
      item.unGuaType = enumUnGuaType[item.un_gua_type]
      item.endDay = deltaDay(this.$utils.dateFormat(new Date(), this.dateFmt), item.endDate)
      return item
    },

    hanleView(row = {}) {
      this.fileData = row
      this.reportVisible = true
    },
    beforeClose() {
      this.reportVisible = false
      setTimeout(() => (this.activeName = 'letter'), 200)
    },
  },
}
</script>
<style lang="scss" scoped>
.status-label {
  display: inline-block;
  padding: 0.25em 0.4em;
  font-size: 75%;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  vertical-align: baseline;
  border-radius: 0.25rem;
  transition: color 0.15s;
}
</style>
